#import "CTLocationManager.h"
static CTLocationManager *manager = nil;
@implementation CTLocationManager
+(instancetype)sharedManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CTLocationManager alloc]init];
    });
    return manager;
}
-(instancetype)init{
    if (self = [super init]) {
        self.delegate = self;
        self.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [self requestWhenInUseAuthorization];
        [self startUpdatingLocation];
        switch (CLLocationManager.authorizationStatus) {
            case kCLAuthorizationStatusNotDetermined: {
                if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                    [manager requestWhenInUseAuthorization];
                }
                [manager startUpdatingLocation];
            }
                break;
            case kCLAuthorizationStatusDenied: {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self showAlert];
                });
            }
                break;
            default:
                break;
        }
    }
    return self;
}
- (void)showAlert {
    __weak typeof(self) weakself = self;
    TJAlertView *aAlertView = [TJAlertView alertWithTitle:@"Prompt" subTitle:@"By locating your region, we can determine the security of your account and give a security reminder. For example: if you are not logged in to use in the registered region or country, we will send a text message reminder"];
    TJAlertViewAction *aCancelAction = [TJAlertViewAction actionWithTitle:@"Cancel" titleColor:[UIColor color161616] backGroudColor:[UIColor whiteColor] handler:^(TJAlertViewAction * _Nonnull action) {
        NSDictionary *data = @{
            @"navigationRoute" : @"CTLaunchImageVC",
            @"pageIdentity" : @"ng_acv_001_00",
            @"elementUniqueIdentifier" : @"ng_acv_001_00",
            @"referenceIdentification" : @"",
        };
        [CTUploadManager ct_uploadType:CTUploadTypePage data:data];
    }];
    TJAlertViewAction *aSureAction = [TJAlertViewAction actionWithTitle:@"Authorized guide" titleColor:[UIColor colorFF6C2E] backGroudColor:[UIColor whiteColor] handler:^(TJAlertViewAction *action) {
        NSDictionary *data = @{
            @"navigationRoute" : @"CTLaunchImageVC",
            @"pageIdentity" : @"ng_acv_001_00",
            @"elementUniqueIdentifier" : @"ng_acv_001_00",
            @"referenceIdentification" : @"",
        };
        [CTUploadManager ct_uploadType:CTUploadTypePage data:data];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [aAlertView doAddAction:aCancelAction];
    [aAlertView doAddAction:aSureAction];
    [aAlertView doShowAlertView];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    self.status = status;
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [manager startUpdatingLocation];
        [CTUploadManager ct_uploadPermissionWithType:CTPermissionTypeLoc permit:YES];
    } else {
        [manager stopUpdatingLocation];
        [CTUploadManager ct_uploadPermissionWithType:CTPermissionTypeLoc permit:NO];
    }
}
- (void)locationManagerDidChangeAuthorization:(CLLocationManager *)manager {
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    self.status = status;
    if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [manager startUpdatingLocation];
        [CTUploadManager ct_uploadPermissionWithType:CTPermissionTypeLoc permit:YES];
    } else {
        [manager stopUpdatingLocation];
        [CTUploadManager ct_uploadPermissionWithType:CTPermissionTypeLoc permit:NO];
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    [manager stopUpdatingLocation];
    CLLocation *location = locations.firstObject;
    self.currentLocation = location;
    [CTUploadManager ct_uploadType:CTUploadTypeLocation];
}
- (CLLocation *)currentLocation {
    if (!_currentLocation) {
        _currentLocation = [[CLLocation alloc] init];
    }
    return _currentLocation;
}
@end
